Skip to main content

Bind privileged port in Linux

· One min read

By default, only root process can bind privileged (1 - 1024) port

Method1: Use setcap

Add capability to process

# sudo setcap 'cap_net_bind_service=+ep' <path of executable>

sudo setcap 'cap_net_bind_service=+ep' /usr/local/bin/nc

Remove capability to process

sudo setcap 'cap_net_bind_service=' /usr/local/bin/nc

or

sudo setcap -r /usr/local/bin/nc

Method 2: Redirect port

Redirect input to other port

Ref: How To Mangle The Packets

iptables -t nat -A PREROUTING [-i <interface>] -p tcp --dport <from port> -j REDIRECT --to-port <to port>